package org.tis.tools.abf.module.ac.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.*;
import org.tis.tools.abf.module.ac.entity.AcApp;
import org.tis.tools.abf.module.ac.service.IAcAppService;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.auth.TisApp;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * acApp的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acApp")
@Validated
@Slf4j
public class AcAppController extends BaseController<AcApp>  {

    @Autowired
    private IAcAppService acAppService;


    @ApiOperation(value = "新增应用",notes = "传入AcAppAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增应用")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(name = "AcAppAddRequest",value = "新增时需要传入的参数",required = true)AcAppAddRequest request){
        if (acAppService.existAddAppCode(request.getAppCode())){
            return ResultVO.error("404", "应用代码已经存在！");
        }
        acAppService.creatRootApp(request.getAppCode(),request.getAppName(),request.getAppType(),request.getUrl(),request.getIpAddr(),request.getIpPort(),request.getAppDesc(),request.getIsopen(),request.getOpenDate());

        return ResultVO.success("新增成功!");
    }

    @ApiOperation(value = "修改应用",notes = "传入AcAppUpdateRequest作为参数")
    @OperateLog(type = OperateType.UPDATE,  desc = "修改应用")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "修改时需要传入的参数",name = "AcAppUpdateRequest",required = true)AcAppUpdateRequest updateRequest) {
        AcApp acApp = acAppService.selectById(updateRequest.getGuid());
        if (acApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (acAppService.existUpdateAppCode(updateRequest.getAppCode(),updateRequest.getGuid())){
            return ResultVO.error("404", "应用代码已经存在！");
        }
        AcApp app = acAppService.changeById(updateRequest.getGuid(),updateRequest.getAppCode(),updateRequest.getAppName(), updateRequest.getAppType(),updateRequest.getIsopen(),updateRequest.getOpenDate() ,updateRequest.getUrl(),updateRequest.getIpAddr(), updateRequest.getIpPort(), updateRequest.getAppDesc());
        return ResultVO.success("修改成功！",app);
    }

    @ApiOperation(value = "删除应用",notes = "传入应用GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "删除应用")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value = "传入应用GUID进行删除",required = true)String id) {
        AcApp acApp = acAppService.selectById(id);
        if (acApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acAppService.moveApp(id);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "根据GUID批量删除应用",notes = "传入AcDeleteListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "根据GUID批量删除应用")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(name = "AcDeleteListRequest",value =
            "批量删除时需要传入的参数",required = true) AcDeleteListRequest listRequest) {
        acAppService.moveBatchApp(listRequest);
        return ResultVO.success("删除成功");

    }

    @ApiOperation(value = "开通应用",notes = "传入应用的GUID(id)作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "开通应用")
    @PutMapping("/openApp/{id}")
    public ResultVO openApp(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value =
            "开通应用时需要传入应用的GUID",required = true) String id){
        AcApp acApp = acAppService.selectById(id);
        if (acApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acApp.setIsopen(YON.YES);
        acApp.setOpenDate(new Date());
        acAppService.updateById(acApp);
        return ResultVO.success("应用已开通",acApp);
    }

    @ApiOperation(value = "停用应用",notes = "传入应用的GUID(id)作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "停用应用")
    @PutMapping("/stopApp/{id}")
    public ResultVO stopApp(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value =
            "停用应用时需要传入应用的GUID",required = true) String id){
        AcApp acApp = acAppService.selectById(id);
        if (acApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acApp.setIsopen(YON.NO);
        acApp.setOpenDate(null);
        acAppService.updateById(acApp);
        return ResultVO.success("应用已停用",acApp);
    }

    @ApiOperation(value = "根据GUID查询应用",notes = "传入应用的GUID(id)作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value =
            "查询时需要传入应用的GUID",required = true) String id) {
        AcApp acApp = acAppService.selectById(id);
        if (acApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acApp);
    }

    @ApiOperation(value = "根据CODE查询应用",notes = "传入应用的CODE(code)作为参数")
    @GetMapping("/auth/{code}")
    public ResultVO detailByCode(@PathVariable @NotBlank(message = "code不能为空") @ApiParam(name = "code",value =
            "查询时需要传入应用的CODE",required = true) String code) {
        EntityWrapper<AcApp> wrapper = new EntityWrapper<>();
        wrapper.eq(AcApp.COLUMN_APP_CODE, code);
        AcApp acApp = acAppService.selectOne(wrapper);
        if (acApp == null) {
            return ResultVO.error("找不到对应记录或已经被删除！");
        }
        TisApp tisApp = new TisApp();
        BeanUtils.copyProperties(acApp, tisApp);
        return ResultVO.success("查询成功", tisApp);
    }


    @ApiOperation(value = "不分页查询所有应用",notes = "不需要传入任何参数")
    @GetMapping("/queryAll")
    public ResultVO queryAll(){
        List<AcApp> list = acAppService.queryAll();
        return ResultVO.success("查询成功",list);
    }


    @ApiOperation(value = "分页查询应用",notes = "需要传入Page,Condition作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(name = "page",value =
            "分页查询需要传入Page,Condition作为参数",required = true) SmartPage<AcApp> page) {
        return  ResultVO.success("查询成功", acAppService.selectPage(getPage(page), getCondition(page)));
    }


    @ApiOperation(value = "批量查询应用信息",notes = "需要传入AcAppListRequest类作为参数")
    @PostMapping("/batchQuery")
    public ResultVO batchQuery(@RequestBody @Validated @ApiParam(name = "AcAppListRequest",value =
            "查询需要传入AcAppListRequest类",required = true) AcAppListRequest acAppListRequest){
        List<AcApp> lists = acAppService.batchQuery(acAppListRequest);
        return ResultVO.success("查询成功",lists);
    }

    @ApiOperation(value = "查看API详情",notes = "传入应用guid为参数")
    @GetMapping("/getSwagger/{guid}")
    public ResultVO getSwagger(@PathVariable @NotBlank(message = "应用guid不能为空") @ApiParam(name = "guid",value =
            "查询需要传入guid",required = true) String guid){
        return ResultVO.success("查询成功",acAppService.getSwagger(guid));
    }

    @ApiOperation(value = "新增应用时应用代码是否重复",notes = "传入应用代码(appCode)作为参数")
    @PostMapping("/existAddAppCode")
    public ResultVO getAddPositionCode(@RequestBody @Validated @ApiParam(value = "AcAppCodeRequest",name = "传入的AcAppCodeRequest类", required = true) AcAppCodeRequest acAppCodeRequest){
        return ResultVO.success("查询成功",acAppService.existAddAppCode(acAppCodeRequest.getAppCode()));
    }

    @ApiOperation(value = "修改应用时应用代码是否重复",notes = "传入应用代码(appCode)和GUID(guid)作为参数")
    @PostMapping("/existUpdateAppCode")
    public ResultVO getUpdatePositionCode(@RequestBody @Validated @ApiParam(value = "AcAppCodeRequest",name = "传入的AcAppCodeRequest类", required = true) AcAppCodeRequest acAppCodeRequest){
        AcApp acApp = acAppService.selectById(acAppCodeRequest.getGuid());
        if (acApp == null){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功",acAppService.existUpdateAppCode(acAppCodeRequest.getAppCode(), acAppCodeRequest.getGuid()));
    }
}

